import React from 'react';
import { connect } from 'react-redux';
import {
	Paper, 
	PaperTitle, 
	PaperContent, 
	PaperFooter,
	PopupWindow,
	BasicTable,
	DefaultPaper} from '../components/dumb';
import { selectedCity,
		 selectedSchool, 
		 selectedCounty, 
		 selectedSubject,
		 selectedGrade,
		 /*获取年级数据*/
	 	 getGradeOptions,
	 	 getSubjectOptions,
	 	 /*获取地域数据*/
	 	 getCityOptions,
	 	 getCountyOptions,
	 	 getCountyOptionsSuccess,
	 	 getSchoolOptions,
	 	 getSchoolOptionsSuccess, } from '../actions/filter';
import { switchSubView } from '../actions/level3Navigator';
import Filter from './Filter';
import { getDetail, selectTeachersInformationPage } from '../actions/teachingJournal'
import { BASIC, CHART } from '../../../constants/basicOverview';
import { CITY, COUNTY, SCHOOL } from '../../../constants/area';

import { AreaSelector,TeachersInformationList, CheckPanel, ListPagesTurnButton } from '../components/smart';

import sorryImg from '../../resources/teachingJournal_sorry.png';


function mapStateToProps(state) {
	let { teachingJournal, filter, level3Navigator, authentication } = state;
	return {
		teachingJournal,
		filter,
		level3Navigator,
		authentication
	};
}

function getAdressLable(city,county,school) {
	let arr = [city,county,school];
	return arr.map((item)=>item.selectedItem.name?item.selectedItem.name:'').join('')
}

/**
 * [根据筛选器刷新页面]
 * @param  {[type]} props [获取props]
 * @param  {[type]} area  [获取当前地域信息]
 * @return {[type]}       [description]
 */

function getData(props){
	let { filter, dispatch, teachingJournal, level3Navigator }=props;
	let { city, county, school, dateRange, subject, grade } =filter;
	let { subView } = level3Navigator;
	let { currentPageIndex } = teachingJournal;
	let params = {
			startDate:dateRange[0],
			endDate:dateRange[1],
			subjects:subject.selectedItems,
			grades:grade.selectedItems,
			pageNum:currentPageIndex-1,
			pageSize:20
		}
			
	if (subView==SCHOOL){
		let schoolParams = Object.assign({},params,{
			schoolId:school.selectedItem.enum
		})
		dispatch(getDetail(schoolParams))
	}
	if (subView==COUNTY){
		let countyParams = Object.assign({},params,{
			countyId:county.selectedItem.enum,
		})
		dispatch(getDetail(countyParams))
	}
	if (subView==CITY){
		let cityParams = Object.assign({},params,{
			cityId:city.selectedItem.enum
		})
		dispatch(getDetail(cityParams))
	}
}

export default class TeachingJournal extends React.Component {
	static propTypes = {
		name: React.PropTypes.string,
	}

	constructor(props) {
		super(props);
	}

/**
	 * 通过计时器等待props改变再执行调用，
	 * 以下代码同理
	 * @type {[type]}
	 */
	getCurrentData = () =>{
		let timer = setTimeout(() => {
			let props = this.props;
			getData(props)
			clearTimeout(timer)
		}, 0)
	}
	
	componentWillMount(){
		this.getCurrentData()
	}
	
	handleFilterChange = () => {
		//切换筛选器时页码回到第一页
		let { dispatch } = this.props;
		dispatch(selectTeachersInformationPage(1)) 

		this.getCurrentData()
	}
	/**
	 * 日志列表翻页
	 * @return {[type]} [description]
	 */
	handleSwitchPageList = (pageIndex) => {
		let { dispatch } = this.props;
		dispatch(selectTeachersInformationPage(pageIndex)) 
		this.getCurrentData()
	}
	
	

	render() {
		
		
		let { teachingJournal, filter, level3Navigator } = this.props;
		let { pageSize, pageIndex, detail, currentPageIndex } = teachingJournal;
		let { city, county, school } = filter;
		let { subView } = level3Navigator;
		let address = getAdressLable(city,county,school);

		return (<div>
				<Filter hasDateSelector={false} hasSwitchDate={false} subView={subView} onFilterChange={this.handleFilterChange} />
				{pageIndex==0&&<DefaultPaper path={sorryImg} primaryText='抱歉，无法找到符合您要求的数据' subTitle='建议您更改筛选条件'/>}
				{pageIndex!=0&&<Paper>
					<PaperTitle>
						<div className='titleBar'>
							<span>{address}教师电子化信息明细</span>
						</div>
					</PaperTitle>
					<PaperContent>
						<TeachersInformationList detail={detail} />
					</PaperContent>
					<PaperFooter>
						<ListPagesTurnButton pageIndex={pageIndex} onPaging={this.handleSwitchPageList} currentPageIndex={currentPageIndex}/>
					</PaperFooter>
				</Paper>}
				</div>)
		}
	}

export default connect(
		mapStateToProps,
	)(TeachingJournal)